package com.example.perpetualcalendar.ui.fragment.almanac;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.perpetualcalendar.data.network.AppSchedulerProvider;
import com.example.perpetualcalendar.domain.entity.Almanac;
import com.example.perpetualcalendar.domain.entity.Calendar;
import com.example.perpetualcalendar.domain.usecase.AlmanacUseCase;
import com.example.perpetualcalendar.viewModel.BaseViewModel;

import io.reactivex.observers.DisposableObserver;

/**
 * @author langhua
 */
public class AlmanacViewModel extends BaseViewModel<AlmanacUseCase> {

    private MutableLiveData<Almanac.ResultDTO> mDto;

    public AlmanacViewModel() {
        mDto = new MutableLiveData<>();
        almanac("");
    }

    public void almanac(String date) {
        assert useCase != null;
        useCase.execute(new DisposableObserver<Almanac>() {
            @Override
            public void onNext(Almanac almanac) {
                if(almanac!=null&&almanac.getResult()!=null){
                    mDto.postValue(almanac.getResult());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        },useCase.almanacApi(date));
    }

    @NonNull
    @Override
    protected AlmanacUseCase setUseCase() {
        return new AlmanacUseCase(AppSchedulerProvider.getInstance(),new AlmanacRepository());
    }

    public LiveData<Almanac.ResultDTO> getDto() {
        return mDto;
    }
}